package IntegerPhenotype;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import BinaryGenotype.BinaryGenotype;
import Generic.*;
import RealGenotype.RealGenotype;

public class IntegerPhenotype implements Phenotype,Iterable<Integer>{
	
	private List<Integer> integers;
	private BinaryGenotype bgType;
	
	public IntegerPhenotype(BinaryGenotype binaryGenotype, int bitsPerInteger)
	{
		bgType = binaryGenotype;
		integers = new ArrayList<Integer>();
		int bits = binaryGenotype.getSize();
		for(int i = 0; i<bits;i+=bitsPerInteger)
		{
			int num = 0;
			for(int j = i; j<i+bitsPerInteger; j++)
			{
				num*=2;
				num+=binaryGenotype.getBit(j);
			}
			integers.add(num);
		}
	}
	
	public String toString()
	{
		String str="";
		
		for(int i = 0; i<integers.size();i++)
			str+=integers.get(i)+((i==integers.size()-1)?"":", ");
		
		return str;
	}

	public int getInt(int index) {
		
		return integers.get(index);
	}

	
	public Iterator<Integer> iterator() {
		return integers.iterator();
	}

	public int getSum() {
		int ret = 0;
		for(Integer i : integers)
			ret+=i;
		return ret;
	}

	public int getSize() {
		return integers.size();
	}

	public Genotype getGenotype() {
		return bgType;
	}
}
